using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using System.ComponentModel;
using System.Xml;

namespace ScottCateAjax2008.Lesson03
{
    /// <summary>
    /// Summary description for E_AutoComplete1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    [ScriptService]
    public class E_AutoComplete1 : WebService
    {

        [WebMethod]
        [ScriptMethod]
        public string[] GetCompletionList(string prefixText, int count)
        {
            XmlDocument data = new XmlDocument();
            string path = HttpContext.Current.Server.MapPath("~/App_data/CityList.xml");
            data.Load(path);

            XmlNodeList cityData = data.GetElementsByTagName("city");

            List<String> foundCities = new List<string>();

            foreach (XmlNode city in cityData)
            {
                string cityName = city.Attributes["name"].Value;

                if (cityName.ToLower().StartsWith(prefixText.ToLower()))
                    foundCities.Add(cityName);

                if (foundCities.Count >= count)
                    break;
            }
            string[] cities = new string[foundCities.Count];

            for (int i = 0; i < foundCities.Count; i++)
            {
                cities[i] = foundCities[i];
            }

            return cities;
        }
    }
}
